package com.lagou.controller;

import com.lagou.pojo.Article;
import com.lagou.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/article")
    public ModelAndView index(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelAndView modelAndView) {
        // 分页参数
        Pageable pageable = PageRequest.of(page, 3, Sort.by(Sort.Direction.DESC, "created"));

        Page<Article> articleList = articleService.findAll(pageable);

        // 设置返回的数据
        modelAndView.setViewName("client/index");
        modelAndView.addObject("articleList", articleList);

        return modelAndView;
    }
}
